package com.idea.cery.springboot1.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author cery
 */
@Api(description = "缓存数据库Redis")
@Controller
@RequestMapping("rc")
public class RedisController {

    @Resource
    private RedisTemplate       redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @ApiOperation(value = "获取Redis值", notes = "例：访问路径 tc/cs?str=123", httpMethod = "GET")
    @RequestMapping(value = "getValue")
    @ResponseBody
    public String getRedisValue(String key) {
        System.out.println(key);
        String str = stringRedisTemplate.opsForValue().get(key);
        return "获取的值：" + str;
    }

    @ApiOperation(value = "设置Redis键值", notes = "例：访问路径 tc/setValue?str=123", httpMethod = "GET")
    @RequestMapping(value = "setValue")
    @ResponseBody
    public String setRedisValue(String key, String value) {
        System.out.println(key + ", " + value);
        stringRedisTemplate.opsForValue().set(key, value);
        return "添加键值对：" + key + ", " + value + " 访问getValue 查看";
    }

    @ApiOperation(value = "删除Redis键值", notes = "例：访问路径 tc/setValue?str=123", httpMethod = "GET")
    @RequestMapping(value = "delValue")
    @ResponseBody
    public String delRedisValue(String key) {
        System.out.println(key);
        stringRedisTemplate.opsForValue().getOperations().delete(key);
        return "删除键值对：" + key + " 访问getValue 查看";
    }
}
